<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `update` (
	`updateid` int(11) NOT NULL auto_increment,
	`date` DATETIME NOT NULL,
	`content` TEXT NOT NULL,
	`crmuserid` int(11) NOT NULL,
	`supportcaseid` int(11) NOT NULL, INDEX(`crmuserid`,`supportcaseid`), PRIMARY KEY  (`updateid`)) ENGINE=MyISAM;
*/

/**
* <b>Update</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @see http://www.phpobjectgenerator.com/plog/tutorials/45/pdo-mysql
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5.1&wrapper=pdo&pdoDriver=mysql&objectName=Update&attributeList=array+%28%0A++0+%3D%3E+%27date%27%2C%0A++1+%3D%3E+%27content%27%2C%0A++2+%3D%3E+%27CRMUser%27%2C%0A++3+%3D%3E+%27SupportCase%27%2C%0A++4+%3D%3E+%27Document%27%2C%0A%29&typeList=array%2B%2528%250A%2B%2B0%2B%253D%253E%2B%2527DATETIME%2527%252C%250A%2B%2B1%2B%253D%253E%2B%2527TEXT%2527%252C%250A%2B%2B2%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B3%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B4%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2529
*/
include_once('class.pog_base.php');
include_once('class.documentupdatemap.php');
class Update extends POG_Base
{
	public $updateId = '';

	/**
	 * @var DATETIME
	 */
	public $date;
	
	/**
	 * @var TEXT
	 */
	public $content;
	
	/**
	 * @var INT(11)
	 */
	public $crmuserId;
	
	/**
	 * @var INT(11)
	 */
	public $supportcaseId;
	
	/**
	 * @var private array of Document objects
	 */
	private $_documentList = array();
	
	public $pog_attribute_type = array(
		"updateId" => array('db_attributes' => array("NUMERIC", "INT")),
		"date" => array('db_attributes' => array("TEXT", "DATETIME")),
		"content" => array('db_attributes' => array("TEXT", "TEXT")),
		"CRMUser" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"SupportCase" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"Document" => array('db_attributes' => array("OBJECT", "JOIN")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function Update($date='', $content='')
	{
		$this->date = $date;
		$this->content = $content;
		$this->_documentList = array();
	}
	
	
	/**
	* Gets object from database
	* @param integer $updateId 
	* @return object $Update
	*/
	function Get($updateId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `update` where `updateid`='".intval($updateId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->updateId = $row['updateid'];
			$this->date = $row['date'];
			$this->content = $this->Unescape($row['content']);
			$this->crmuserId = $row['crmuserid'];
			$this->supportcaseId = $row['supportcaseid'];
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $updateList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `update` ";
		$updateList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "updateid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$update = new $thisObjectName();
			$update->updateId = $row['updateid'];
			$update->date = $row['date'];
			$update->content = $this->Unescape($row['content']);
			$update->crmuserId = $row['crmuserid'];
			$update->supportcaseId = $row['supportcaseid'];
			$updateList[] = $update;
		}
		return $updateList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $updateId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `updateid` from `update` where `updateid`='".$this->updateId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `update` set 
			`date`='".$this->date."', 
			`content`='".$this->Escape($this->content)."', 
			`crmuserid`='".$this->crmuserId."', 
			`supportcaseid`='".$this->supportcaseId."'where `updateid`='".$this->updateId."'";
		}
		else
		{
			$this->pog_query = "insert into `update` (`date`, `content`, `crmuserid`, `supportcaseid`) values (
			'".$this->date."', 
			'".$this->Escape($this->content)."', 
			'".$this->crmuserId."', 
			'".$this->supportcaseId."')";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->updateId == "")
		{
			$this->updateId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_documentList as $document)
			{
				$document->Save();
				$map = new DocumentUpdateMap();
				$map->AddMapping($this, $document);
			}
		}
		return $this->updateId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $updateId
	*/
	function SaveNew($deep = false)
	{
		$this->updateId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($across)
		{
			$documentList = $this->GetDocumentList();
			$map = new DocumentUpdateMap();
			$map->RemoveMapping($this);
			foreach ($documentList as $document)
			{
				$document->Delete($deep, $across);
			}
		}
		else
		{
			$map = new DocumentUpdateMap();
			$map->RemoveMapping($this);
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `update` where `updateid`='".$this->updateId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `update` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Associates the CRMUser object to this one
	* @return boolean
	*/
	function GetCrmuser()
	{
		$crmuser = new CRMUser();
		return $crmuser->Get($this->crmuserId);
	}
	
	
	/**
	* Associates the CRMUser object to this one
	* @return 
	*/
	function SetCrmuser(&$crmuser)
	{
		$this->crmuserId = $crmuser->crmuserId;
	}
	
	
	/**
	* Associates the SupportCase object to this one
	* @return boolean
	*/
	function GetSupportcase()
	{
		$supportcase = new SupportCase();
		return $supportcase->Get($this->supportcaseId);
	}
	
	
	/**
	* Associates the SupportCase object to this one
	* @return 
	*/
	function SetSupportcase(&$supportcase)
	{
		$this->supportcaseId = $supportcase->supportcaseId;
	}
	
	
	/**
	* Creates mappings between this and all objects in the Document List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetDocumentList(&$documentList)
	{
		$map = new DocumentUpdateMap();
		$map->RemoveMapping($this);
		$this->_documentList = $documentList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $updateList
	*/
	function GetDocumentList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$document = new Document();
		$documentList = Array();
		$this->pog_query = "select distinct * from `document` a INNER JOIN `documentupdatemap` m ON m.documentid = a.documentid where m.updateid = '$this->updateId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($document->pog_attribute_type[$sortBy]['db_attributes']) && $document->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $document->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.documentid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$document = new Document();
			foreach ($document->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$document->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$document->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$documentList[] = $document;
		}
		return $documentList;
	}
	
	
	/**
	* Associates the Document object to this one
	* @return 
	*/
	function AddDocument(&$document)
	{
		if ($document instanceof Document)
		{
			if (in_array($this, $document->updateList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_documentList as $document2)
				{
					if ($document->documentId > 0 && $document->documentId == $document2->documentId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_documentList[] = $document;
				}
			}
		}
	}
}
?>